/**
 * Copyright (C), 2020-2021 by colley.ma
 * File Name: RestQuery.java
 * Encoding: UTF-8
 * Date: 2021年3月17日 下午3:27:46
 * History:
*/
package com.swak.frame.dto.request;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.swak.frame.util.DFormatEnum;
import com.swak.frame.util.DateTimeUtils;
import com.swak.frame.util.GetterUtil;

/**
 * HashMap request 获取前端传递参数
 * ClassName: MapRequest.java 
 * @author colley.ma
 * @date 2021年3月17日 下午3:32:21
 */
public class MapRequest extends HashMap<String, Object> {

	private static final long serialVersionUID = 6710817839970870796L;

	public String getString(String key) {
		return getString(key, null);
	}

	public String getString(String key, String defaultVal) {
		Object obj = super.get(key);

		if (obj != null) {
			return obj.toString();
		}

		return defaultVal;
	}

	public List<Long> getLongs(String param) {
		if (StringUtils.isNotEmpty(param)) {
			return GetterUtil.getSplit2Long(param);
		}
		return new ArrayList<Long>();
	}

	public List<Integer> getIntegers(String name) {
		String param = getString(name);

		if (StringUtils.isNotEmpty(param)) {
			return GetterUtil.getSplit2Integer(param);
		}

		return new ArrayList<Integer>();
	}

	public Date getDate(String paramName, String fmt) {
		if (StringUtils.isEmpty(fmt)) {
			fmt = DFormatEnum.YYYY_MM_DD.pattern;
		}

		String endDateStr = getString(paramName);

		if (StringUtils.isNotEmpty(endDateStr)) {
			return DateTimeUtils.string2Date(endDateStr, fmt);
		}

		return null;
	}

	public Date getStartDate(String paramName) {
		String endDateStr = getString(paramName);

		if (StringUtils.isNotEmpty(endDateStr)) {
			endDateStr = endDateStr + " 00:00:00";

			Date endDate = DateTimeUtils.string2Date(endDateStr, DFormatEnum.YYYY_MM_DDHH_MM_SS.pattern);

			if (endDate != null) {
				return endDate;
			}
		}

		return null;
	}

	public Date getEndDate(String paramName) {
		String endDateStr = getString(paramName);

		if (StringUtils.isNotEmpty(endDateStr)) {
			Date endDate = DateTimeUtils.string2Date(endDateStr, DFormatEnum.YYYY_MM_DD.pattern);

			if (endDate != null) {
				endDate = DateUtils.addHours(endDate, 23);
				endDate = DateUtils.addMinutes(endDate, 59);
				endDate = DateUtils.addSeconds(endDate, 59);

				return new Date(endDate.getTime());
			}
		}

		return null;
	}

	public Integer getInteger(String key) {
		return getInteger(key, null);
	}

	public Integer getInteger(String name, Integer defaultVal) {
		String param = getString(name);

		return GetterUtil.getInteger(param, defaultVal);
	}

	public Long getLong(String key) {
		return getLong(key, null);
	}

	public Long getLong(String key, Long defaultVal) {
		String value = getString(key);

		return GetterUtil.getLong(value, defaultVal);
	}
}
